package com.carlinfo.stu_ques.question.dao.impl;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.common.dao.impl.BaseDaoImpl;
import com.carlinfo.stu_ques.demo.pojo.Dynasty;
import com.carlinfo.stu_ques.question.dao.IQuestionDao;
import com.carlinfo.stu_ques.question.pojo.Question;

public class QuestionDaoImpl extends BaseDaoImpl<Question> implements IQuestionDao 
{

	/* 主键不能重复 */
	// private static int id;
	/*
	 * 找一个容器;放的是问题,实现CRUD Map:通过键快速找到值 键是id(唯一标识),值:POJO;
	 * 
	 */
	// private Map<Integer, Question> questionMap = new TreeMap<Integer, Question>();
	// private FileUtil fileUtil = new FileUtil();
	private File questionFile = new File(this.parentFile, "question.dat");

	/**
	 * id自加,这个id自加,只需要自己使用
	 *
	 */
	/*
	 * private void idCount() { id++; }
	 */

	@Override
	public int saveOneDao(Question question)
	{
		// this.idCount();
		question.setId(id);
		// this.questionMap.put(question.getId(), question);
		this.tempId = question.getId();
		int resultId = super.saveOneDao(question);
		writeFile(questionFile);
		return resultId;
	}
	/* 将Map中的内容写到文件中 */

	// public void writeFile()
	// {
	// try
	// {
	// if (!questionFile.getParentFile().exists())
	// {
	// questionFile.getParentFile().mkdirs();
	// }
	// /* 准备一根管子,插到文件上 */
	// FileOutputStream fos = new FileOutputStream(questionFile);
	// List<Object> dataList = new ArrayList<Object>();
	// dataList.add(this.questionMap);
	// dataList.add(this.id);
	// if (!this.fileUtil.writeObject(fos, dataList))
	// {
	// System.out.println("==我存储失败了==");
	// }
	// } catch (FileNotFoundException e)
	// {
	// e.printStackTrace();
	// }
	// }

	@Override
	public void updateOneDao(Question question)
	{
		/* 如何换掉 Map如何更新 */
		// this.questionMap.put(question.getId(), question);
		this.tempId = question.getId();

		super.updateOneDao(question);
		writeFile(questionFile);
	}
	@Override
	public void deleteOneDao(Question question)
	{
		/* 删除 */
		// this.questionMap.remove(question.getId());
		this.tempId = question.getId();

		super.deleteOneDao(question);
		// writeFile();
		writeFile(questionFile);

	}

	public QuestionDaoImpl()
	{
		readFile(questionFile);;
	}

	// @Override
	// public List<Question> selectListDao()
	// {
	// List<Question> list = new ArrayList<Question>();
	// Set<Entry<Integer, Question>> entrySet = this.questionMap.entrySet();
	// for (Iterator iterator = entrySet.iterator(); iterator.hasNext();)
	// {
	// Entry<Integer, Question> entry = (Entry<Integer, Question>) iterator.next();
	// int key = entry.getKey();
	// Question question = entry.getValue();
	// list.add(question);
	// }
	// return list;
	// }
	//
	// @Override
	// public Question selectOneDao(Map<String, Object> condMap)
	// {
	// if (condMap.get("id") != null)
	// {
	// Integer id = (Integer) condMap.get("id");
	// return this.questionMap.get(id);
	// }
	// return null;
	// }

}
